﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace V5_Common {
    public class PagerHelper {
        private static readonly PagerHelper m_Instance = new PagerHelper();
        /// <summary>
        /// 
        /// </summary>
        public static PagerHelper Instance {
            get { return m_Instance; }
        }
        /// <summary>
        /// 分页大小
        /// </summary>
        public int PageSize { get; set; }
        /// <summary>
        /// 总记录数
        /// </summary>
        public int RecordCount { get; set; }
        /// <summary>
        /// 当前页索引
        /// </summary>
        public int CurrentPageIndex { get; set; }
        /// <summary>
        /// 分页格式化参数 {0}分页页数
        /// </summary>
        public string PageUrlFormat { get; set; }
        /// <summary>
        /// 显示分页信息
        /// </summary>
        public string CustomInfoHTML { get; set; }
        /// <summary>
        /// 分页数据源
        /// </summary>
        public object DataSource { get; set; }


        //private string m_Display = string.Empty;
        ///// <summary>
        ///// 显示分页
        ///// </summary>
        //public string Display { get { return m_Display; } set { this.m_Display = value; } }
        /// <summary>
        /// 
        /// </summary>
        public PagerHelper() {
        }
        /// <summary>
        /// 显示分页数据
        /// </summary>
        /// <returns></returns>
        public string Display() {
            int stepNum = 4;
            int pageRoot = 1;
            int pageCount = 0;
            pageCount = this.RecordCount / this.PageSize;
            if (this.RecordCount % this.PageSize > 0) {
                pageCount++;
            }
            pageCount = pageCount == 0 ? 1 : pageCount;
            this.CurrentPageIndex = this.CurrentPageIndex == 0 ? 1 : this.CurrentPageIndex;

            StringBuilder sb = new StringBuilder();
            sb.Append(@"<table cellpadding=""0"" cellspacing=""1"" class=""pager""><tr>");
            sb.Append(@"<td class=""pagerTitle"">&nbsp;分页&nbsp;</td>");
            sb.Append(@"<td class=""pagerTitle"">&nbsp;" + this.CurrentPageIndex.ToString() + "/" + pageCount.ToString() + "&nbsp;</td>\r");
            if (this.CurrentPageIndex - stepNum < 2)
                pageRoot = 1;
            else
                pageRoot = this.CurrentPageIndex - stepNum;
            int pageFoot = pageCount;
            if (this.CurrentPageIndex + stepNum >= pageCount)
                pageFoot = pageCount;
            else
                pageFoot = this.CurrentPageIndex + stepNum;

            if (pageRoot == 1) {
                if (this.CurrentPageIndex > 1) {
                    sb.Append("<td>&nbsp;<a href='" + string.Format(this.PageUrlFormat, 1) + "' title='首页'>首页</a>&nbsp;</td>\r");
                    sb.Append("<td>&nbsp;<a href='" + string.Format(this.PageUrlFormat, this.CurrentPageIndex - 1) + "' title='上页'>上页</a>&nbsp;</td>\r");
                }
            }
            else {
                sb.Append("<td>&nbsp;<a href='" + string.Format(this.PageUrlFormat, 1) + "' title='首页'>首页</a>&nbsp;</td>");
                sb.Append("<td>&nbsp;<a href='" + string.Format(this.PageUrlFormat, this.CurrentPageIndex - 1) + "' title='上页'>上页</a>&nbsp;</td>\r");
            }
            for (int i = pageRoot; i <= pageFoot; i++) {
                if (i == this.CurrentPageIndex) {
                    sb.Append("<td class='current'>&nbsp;" + i.ToString() + "&nbsp;</td>\r");
                }
                else {
                    sb.Append("<td>&nbsp;<a href='" + string.Format(this.PageUrlFormat, i) + "' title='第" + i.ToString() + "页'>" + i.ToString() + "</a>&nbsp;</td>\r");
                }
                if (i == pageCount)
                    break;
            }
            if (pageFoot == pageCount) {
                if (pageCount > this.CurrentPageIndex) {
                    sb.Append("<td>&nbsp;<a href='" + string.Format(this.PageUrlFormat, this.CurrentPageIndex + 1) + "' title='下页'>下页</a>&nbsp;</td>\r");
                    sb.Append("<td>&nbsp;<a href='" + string.Format(this.PageUrlFormat, pageCount) + "' title='尾页'>尾页</a>&nbsp;</td>\r");
                }
            }
            else {
                sb.Append("<td>&nbsp;<a href='" + string.Format(this.PageUrlFormat, this.CurrentPageIndex + 1) + "' title='下页'>下页</a>&nbsp;</td>\r");
                sb.Append("<td>&nbsp;<a href='" + string.Format(this.PageUrlFormat, pageCount) + "' title='尾页'>尾页</a>&nbsp;</td>\r");
            }
            sb.Append("</tr>\r</table>");
            return sb.ToString();
        }

        /// <summary>
        /// 分页sql语句生成代码
        /// </summary>
        /// <param name="SelectFields"></param>
        /// <param name="TblName"></param>
        /// <param name="FldName">排序字段,唯一性</param>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="OrderType"></param>
        /// <param name="whereStr"></param>
        /// <returns></returns>
        public string GetPagerSql(string SelectFields, string TblName, string FldName, int PageSize, int PageIndex, string OrderType, string whereStr) {
            string StrTemp = "";
            string StrSql = "";
            string StrOrder = "";
            //根据排序方式生成相关代码
            if (OrderType.ToUpper() == "ASC") {
                StrTemp = "> (SELECT MAX(" + FldName + ")";
                StrOrder = " ORDER BY " + FldName + " ASC";
            }
            else {
                StrTemp = "< (SELECT MIN(" + FldName + ")";
                StrOrder = " ORDER BY " + FldName + " DESC";
            }
            PageIndex = int.Parse("0" + PageIndex.ToString());
            PageIndex = PageIndex == 0 ? 1 : PageIndex;
            //若是第1页则无须复杂的语句
            if (PageIndex == 1) {
                StrTemp = "";
                if (whereStr != "")
                    StrTemp = " Where " + whereStr;
                StrSql = "SELECT TOP " + PageSize + " " + SelectFields + " From " + TblName + "" + StrTemp + StrOrder;
            }
            else {
                //若不是第1页，构造sql语句
                StrSql = "SELECT TOP " + PageSize + " " + SelectFields + " From " + TblName + " WHERE " + FldName + "" + StrTemp + " From (SELECT TOP " + (PageIndex - 1) * PageSize + " " + FldName + " From " + TblName + "";
                if (whereStr != "")
                    StrSql += " Where " + whereStr;
                StrSql += StrOrder + ") As Tbltemp)";
                if (whereStr != "")
                    StrSql += " And " + whereStr;
                StrSql += StrOrder;
            }
            //返回sql语句
            return StrSql;
        }

        /// <summary>
        /// 连表分页sql语句生成代码
        /// <param name="SelectFields">连表查询的字段</param>
        /// <param name="TblNameA">表1</param>
        /// <param name="TblNameB">表2</param>
        /// <param name="FldName">表1的排序字段名</param>
        /// <param name="PageSize">每页记录数</param>
        /// <param name="PageIndex">当前页</param>
        /// <param name="OrderType">排序方式：DESC ASC</param>
        /// <param name="joinStr">连接条件</param>
        /// <param name="whereStr1">外围条件带A.</param>
        /// <param name="whereStr2">分页条件:不带A.</param>
        /// </summary>
        public string GetPagerSql(string SelectFields, string TblNameA, string TblNameB, string FldName, int PageSize, int PageIndex, string OrderType, string joinStr, string whereStr1, string whereStr2) {
            string StrTemp = "";
            string StrSql = "";
            string StrOrder1 = "";
            string StrOrder2 = "";
            //根据排序方式生成相关代码
            if (OrderType.ToUpper() == "ASC") {
                StrTemp = "> (SELECT MAX(" + FldName + ")";
                StrOrder1 = " ORDER BY A." + FldName + " ASC";
                StrOrder2 = " ORDER BY " + FldName + " ASC";
            }
            else {
                StrTemp = "< (SELECT MIN(" + FldName + ")";
                StrOrder1 = " ORDER BY A." + FldName + " DESC";
                StrOrder2 = " ORDER BY " + FldName + " DESC";
            }
            PageIndex = int.Parse("0" + PageIndex.ToString());
            PageIndex = PageIndex == 0 ? 1 : PageIndex;
            //若是第1页则无须复杂的语句
            if (PageIndex == 1) {
                StrTemp = "";
                if (whereStr1 != "")
                    StrTemp = " WHERE " + whereStr1;
                StrSql = "SELECT TOP " + PageSize + " " + SelectFields + " FROM [" + TblNameA + "] A LEFT JOIN [" + TblNameB + "] B on " + joinStr + " " + StrTemp + StrOrder1;
            }
            else {
                //若不是第1页，构造sql语句
                StrSql = "SELECT TOP " + PageSize + " " + SelectFields + " FROM [" + TblNameA + "] A LEFT JOIN [" + TblNameB + "] B on " + joinStr + " WHERE A." + FldName + "" + StrTemp + " From (SELECT TOP " + (PageIndex - 1) * PageSize + " " + FldName + " From [" + TblNameA + "] ";
                if (whereStr2 != "")
                    StrSql += " Where " + whereStr2;
                StrSql += StrOrder2 + ") As Tbltemp)";
                if (whereStr1 != "")
                    StrSql += " And " + whereStr1;
                StrSql += StrOrder1;
            }
            //返回sql语句
            return StrSql;
        }
    }
}
